package com.greateamd.timetableasy.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import net.sf.gilead.pojo.gwt.LightEntity;

@SuppressWarnings("serial")
@Entity
@Table(name="universities")
public class University extends LightEntity implements PersistableEntity,HasEventsEntity,Serializable
{

	@Id
	@GeneratedValue
	private Integer id;
	
	@Column(unique=true,nullable=false)
	private String name;
	@Column(nullable=false)
	private String address;
	
	@Column(name="enabled",nullable=false)
	private boolean enabled = true;

	@OneToMany(mappedBy="university", fetch=FetchType.LAZY)
	private Set<Cursus> cursuses;
	
	@OneToMany(mappedBy="university", fetch=FetchType.LAZY)
	private Set<Campus> campuses;
	
	@ManyToMany(fetch = FetchType.LAZY,targetEntity=Event.class,cascade=CascadeType.ALL)
    @JoinTable(name="universities_events",
		        joinColumns = @JoinColumn(columnDefinition="university"),
		        inverseJoinColumns=@JoinColumn(name="event_id"))
	private Set<Event> events;
	
	public University()
	{}

	public University(String name) 
	{
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getAddress() {
		return address;
	}

	public Set<Campus> getCampuses() {
		return campuses;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setCampuses(Set<Campus> campuses) {
		this.campuses = campuses;
	}

	public Set<Cursus> getCursuses() {
		return cursuses;
	}

	public void setCursuses(Set<Cursus> cursuses) {
		this.cursuses = cursuses;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	public Set<Event> getEvents() {
		return events;
	}

	public void setEvents(Set<Event> events) {
		this.events = events;
	}

	public void addEvent(Event event) {
		if (events == null)
			events = new HashSet<Event>();
		this.events.add(event);
	}

	public void addCampus(Campus campus) {
		if (campuses == null)
			campuses = new HashSet<Campus>();
		this.campuses.add(campus);
	}
	
	public void addCursus(Cursus cursus) {
		if (cursuses == null)
			cursuses = new HashSet<Cursus>();
		this.cursuses.add(cursus);
	}
	
	@Override
	public String toString() {
		return getName() + " "+ getAddress();
	}
}
